/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "componenttable.h"
#include <iostream>

ComponentTable::ComponentTable()
: wxGridTableBase()
{
	//std::cerr<<"ComponentTable::ComponentTable()"<<std::endl;
	SetBoard(NULL);
}

ComponentTable::ComponentTable(Board *board)
: wxGridTableBase()
{
	//std::cerr<<"ComponentTable::ComponentTable()"<<std::endl;
	SetBoard(board);
}

ComponentTable::~ComponentTable()
{
	//std::cerr<<"ComponentTable::~ComponentTable()"<<std::endl;
}

void ComponentTable::SetBoard(Board *board)
{
	m_board = board;
	
	m_components.clear();
	if(m_board){
		m_board->GetComponents(m_components);
	}
	
	if(GetView()){
		wxGridTableMessage msg1(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0,
				GetView()->GetNumberRows());
		GetView()->ProcessTableMessage(msg1);
		
		if(m_board){
			wxGridTableMessage msg2(this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, 0,
					m_components.size());
			GetView()->ProcessTableMessage(msg2);
		}
	}
}

int ComponentTable::GetNumberRows()
{
	return m_components.size();
}

int ComponentTable::GetNumberCols()
{
	return 2;
}

wxString ComponentTable::GetValue(int row, int col)
{
	if(!m_board) return wxT("invalid board");
	if(row >= (int)m_components.size()) return wxT("invalid row");
	if(col==0) return m_components[row]->GetRefdes();
	if(col==1) return m_components[row]->GetValue();
	return wxT("---");
}

void ComponentTable::SetValue(int row, int col, const wxString &s)
{
	if(!m_board) return;
	if(row >= (int)m_components.size()) return;
	if(col==0) m_components[row]->SetRefdes(s);
	if(col==1) m_components[row]->SetValue(s);
}

bool ComponentTable::IsEmptyCell(int row, int col)
{
	if(!m_board) return true;
	if(row >= (int)m_components.size()) return true;
	if(col==0) return m_components[row]->GetRefdes().IsEmpty();
	if(col==1) return m_components[row]->GetValue().IsEmpty();
	return true;
}

wxString ComponentTable::GetColLabelValue(int col)
{
	if(col==0) return wxT("refdes");
	if(col==1) return wxT("value");
	return wxT("");
}

Component* ComponentTable::GetComponent(int row)
{
	if(row<0) return NULL;
	if(row >= (int)m_components.size()) return NULL;
	return m_components[row];
}


